#include <stdio.h>
#include <stdlib.h>

// 定义链栈节点结构体
typedef struct StackNode {
    int data;
    struct StackNode *next;
} StackNode;

// 创建一个空栈
StackNode *createStack() {
    return NULL;
}

// 判断栈是否为空
int isEmpty(StackNode *stack) {
    return stack == NULL;
}

// 入栈操作
void push(StackNode **stack, int data) {
    StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
    if (newNode == NULL) {
        printf("内存分配失败\n");
        return;
    }
    newNode->data = data;
    newNode->next = *stack;
    *stack = newNode;
}

// 出栈操作
int pop(StackNode **stack) {
    if (isEmpty(*stack)) {
        printf("栈为空\n");
        return -1;
    }
    StackNode *temp = *stack;
    int data = temp->data;
    *stack = temp->next;
    free(temp);
    return data;
}

// 打印栈元素
void printStack(StackNode *stack) {
    StackNode *current = stack;
    while (current!= NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

int main() {
    StackNode *stack = createStack();

    push(&stack, 10);
    push(&stack, 20);
    push(&stack, 30);

    printf("栈元素: ");
    printStack(stack);

    int poppedData = pop(&stack);
    if (poppedData!= -1) {
        printf("弹出元素: %d\n", poppedData);
    }

    printf("栈元素: ");
    printStack(stack);

    return 0;
}